﻿

CREATE VIEW [dbo].[fares_Facturi]
AS
SELECT     a.IDPartener, a.NrDoc, a.DataDoc, ROUND(a.TotalValoare, 2) AS TotalValoare, ROUND(a.TotalValoareTVA, 2) AS TotalValoareTVA, ROUND(a.Total, 2) AS Total, 
                      COALESCE (b.Valoare, 0) AS Valoare, ROUND(a.Total, 2) - COALESCE (b.Valoare, 0) AS RestPlata, a.DataScadenta, dbo.Parteneri.Denumire, a.IDTipDocument, 
                      a.CodUnitate, CAST(a.IDDocument AS varchar(50)) AS IDDocument, a.CotaTVA
FROM         dbo.Documente AS a LEFT OUTER JOIN
                          (SELECT     IDDocument, SUM(Valoare) AS Valoare
                            FROM          dbo.DocumentePlataDetaliu
                            GROUP BY IDDocument) AS b ON a.IDDocument = b.IDDocument INNER JOIN
                      dbo.Parteneri ON a.IDPartener = dbo.Parteneri.IDPartener